require('../css/invite_friend_award16521.css');
;
$(function() {
    var baseUrl = 'http://activity.gegejia.com/activity/inviteFriends/',
        // var baseUrl = 'http://114.215.198.55:11111/activityTest/inviteFriends/',
        alertElement = document.getElementById('dialogAlert'),
        accountId = getUrlParam('accountId');
    var count = 1, clicked = false;
    getInviteRecod();
    getRankList(1);
    getRankList(2);
    getWinnerList();
    $('.tab-link').click(function() {
        $('.tab-link').removeClass('active');
        $('.tabs .tab').removeClass('active');
        var index = $(this).index();
        $(this).addClass('active');
        $('.tabs .tab').eq(index).addClass('active');
        clicked = true;
        if (index == 2) {
            getWinnerList();
        } else {
            getRankList(index+1);
        }
    });

    $('.J_alert').on('click', function() {
        $('.invite-rewards-modal').addClass('modal-in');
        $('.modal-overlay').addClass('modal-overlay-visible');
    });
    $('.modal-close').on('click', function() {
        $('.invite-rewards-modal').removeClass('modal-in');
        $('.modal-overlay').removeClass('modal-overlay-visible');
    });

    // 获取邀请信息
    function getInviteRecod() {
        var params = {
            accountId: accountId
        };
        ajaxJsonp(baseUrl + 'getInviteRecod', params, cbInviteRecod, count);
    }

    // 获取排行榜
    function getRankList(type) {
        if (!type) type = 1;
        var params = {
            accountId: accountId,
            type: type
        };
        if (type == 1) {
            ajaxJsonp(baseUrl + 'rankList', params, cbTodayRankList, count);
        } else {
            ajaxJsonp(baseUrl + 'rankList', params, cbWeekRankList, count);
        }
    }

    // 获奖名单
    function getWinnerList() {
        var params = {};
        ajaxJsonp(baseUrl + 'winnerList', params, cbWinnerList, count);
    }

    // 获取邀请信息回调
    function cbInviteRecod(result) {
        $('#totalPrize').text(result.totalPrize);
        var inviteHtml = '',
            inviteData = result.inviteList,
            champion = '<span class="trophy-avatar" style="background: url(' + result.champion.headImage + ') no-repeat center center;background-size: cover;"></span><span class="trophy-mobile">' + result.champion.nickname + '</span><span class="trophy-level"><em>V</em><font>' + result.champion.level + '</font></span>';
        $('#champion').html(champion);
        for (i in inviteData) {
            if (i < 5) {
                var inviteTime = '' + inviteData[i].dateTime;
                inviteHtml += '<li>\
                            <div class="my-invite-friend">\
                                <p><span class="friend-nickname">' + inviteData[i].nickname + '</span><span class="invite-time">' + inviteTime + '</span></p>\
                                <p><span class="invite-relation">' + inviteData[i].sourceType + '</span></p>\
                            </div>\
                            <div class="rewards-sum">' + inviteData[i].money + '元</div>\
                        </li>';
            }
        }
        $('#inviteList').html(inviteHtml);
        var _all = $('#inviteAllList');
        if (inviteData.length < 6) {
            if (!_all.hasClass('hide')) $('#inviteAllList').addClass('hide');
            var len = inviteData.length != 0 ? (+inviteData.length - 1) : 0;
            $('#inviteList li').eq(len).addClass('no-border-bottom');
        } else {
            if (_all.hasClass('hide')) $('#inviteAllList').removeClass('hide');
            _all.click(function() {
                var params = {
                    accountId: accountId
                };
                clicked = true;
                ajaxJsonp(baseUrl + 'getInviteRecod', params, cbAllInviteRecod, count);
            });
        }
    }

    // 获取全部邀请信息回调
    function cbAllInviteRecod(result) {
        $('#inviteAllList').hide();
        var inviteHtml = '',
            inviteData = result.inviteList;
        for (i in inviteData) {
            if (i > 4) {
                var inviteTime = '' + inviteData[i].dateTime;
                inviteHtml += '<li>\
                            <div class="my-invite-friend">\
                                <p><span class="friend-nickname">' + inviteData[i].nickname + '</span><span class="invite-time">' + inviteTime + '</span></p>\
                                <p><span class="invite-relation">' + inviteData[i].sourceType + '</span></p>\
                            </div>\
                            <div class="rewards-sum">' + inviteData[i].money + '元</div>\
                        </li>';
            }
        }
        $('#inviteList').append(inviteHtml);
        $('#inviteList li').last().addClass('no-border-bottom');
        if(clicked) onCompletion();
    }

    // 获取当日排行排行榜回调
    function cbTodayRankList(result) {
        var rankHtml = '',
            rankData = result.rankList;
        for (i in rankData) {
            var order = +i + 1;
            if (i > 0) {
                rankHtml += '<li>\
                                <div class="rank-order no-crown">' + order + '.</div>\
                                <div class="rank-member"><span class="nickname">' + rankData[i].nickname + '</span><span class="level"><em>V</em><font>' + rankData[i].level + '</font></span></div>\
                                <div class="invite-count">邀请' + rankData[i].amount + '名小伙伴</div>\
                            </li>';
            } else {
                rankHtml += '<li>\
                                <div class="rank-order">\
                                    <p><span class="crown"></span></p>\
                                    <p><span class="order">' + order + '.</span></p>\
                                </div>\
                                <div class="rank-member"><span class="nickname">' + rankData[i].nickname + '</span><span class="level"><em>V</em><font>' + rankData[i].level + '</font></span></div>\
                                <div class="invite-count">邀请' + rankData[i].amount + '名小伙伴</div>\
                            </li>';
            }
        }
        $('#todayRankList').html(rankHtml);
        var myRankText = '我的当日排行：' + result.rank + '名/' + result.total + '人参与';
        $('#todayMyRank').text(myRankText);
        if(clicked) onCompletion();
    }

    // 获取本周排行排行榜回调
    function cbWeekRankList(result) {
        var rankHtml = '',
            rankData = result.rankList;
        for (i in rankData) {
            var order = +i + 1;
            if (i > 0) {
                rankHtml += '<li>\
                                <div class="rank-order no-crown">' + order + '.</div>\
                                <div class="rank-member"><span class="nickname">' + rankData[i].nickname + '</span><span class="level"><em>V</em><font>' + rankData[i].level + '</font></span></div>\
                                <div class="invite-count">邀请' + rankData[i].amount + '名小伙伴</div>\
                            </li>';
            } else {
                rankHtml += '<li>\
                                <div class="rank-order">\
                                    <p><span class="crown"></span></p>\
                                    <p><span class="order">' + order + '.</span></p>\
                                </div>\
                                <div class="rank-member"><span class="nickname">' + rankData[i].nickname + '</span><span class="level"><em>V</em><font>' + rankData[i].level + '</font></span></div>\
                                <div class="invite-count">邀请' + rankData[i].amount + '名小伙伴</div>\
                            </li>';
            }
        }
        $('#weekRankList').html(rankHtml);
        var myRankText = '我的本周当前排名：' + result.rank + '名/' + result.total + '人参与';
        $('#weekMyRank').text(myRankText);
        if(clicked) onCompletion();
    }

    // 获奖名单回调
    function cbWinnerList(result) {
        var winnerHtml = '',
            winnerData = result.winnerList;
        for (i in winnerData) {
            if (i > 0) {
                if (winnerData[i].status == 1 || winnerData[i].status == '1') {
                    winnerHtml += '<li>\
                                <div class="no-lottery-trophy"></div>\
                                <div class="lottery-status">\
                                    <p><span class="status">' + winnerData[i].nickname + '</span><span class="level"><em>V</em><font>' + winnerData[i].level + '</font></span></p>\
                                    <p class="time">' + winnerData[i].dateTime + '</p>\
                                </div>\
                                <div class="cash-coupon"><span class="amount">' + winnerData[i].award + '<em>元</em></span></div>\
                            </li>';
                } else {
                    winnerHtml += '<li>\
                                <div class="no-lottery-trophy"></div>\
                                <div class="lottery-status">\
                                    <p><span class="status">' + winnerData[i].nickname + '</span></p>\
                                    <p class="time">' + winnerData[i].dateTime + '</p>\
                                </div>\
                                <div class="cash-coupon"><span class="amount">' + winnerData[i].award + '<em>元</em></span></div>\
                            </li>';
                }
            } else {
                winnerHtml += '<li class="week-first">\
                                <div class="lottery-trophy"></div>\
                                <div class="lottery-status">\
                                    <p><span class="status">' + winnerData[i].nickname + '</span></p>\
                                    <p class="time">' + winnerData[i].dateTime + '</p>\
                                </div>\
                                <div class="cash-coupon"><span class="amount">' + winnerData[i].award + '<em>元</em></span></div>\
                            </li>';
            }
        }
        $('#winnerList').html(winnerHtml);
        if(clicked) onCompletion();
    }

    // 错误处理
    function error(result) {
        if (result.message && typeof result.message != 'undefined') {
            alertElement.innerText = result.message;
            if (typeof alertElement != 'undefined') {
                showDialog(alertElement);
            }
        }
    }

    //提示弹框
    function showDialog(ele) {
        ele.setAttribute('style', "opacity: 1;visibility: visible;");
        setTimeout(function() {
            ele.setAttribute('style', "opacity: 0;visibility: hidden;");
        }, 1500)
    }


    // ajax封装
    function ajaxJsonp(url, data, cb, num) {
        $.ajax({
            url: url,
            dataType: 'jsonp',
            data: data,
            contentType: 'application/jsonp; charset=utf-8',
            jsonp: 'callback',
            success: function(result) {
                if (result.code == 1) {
                    cb(result);
                } else {
                    error(result);
                }
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                num++;
                if (num > 3) {
                    alertElement.innerText = '后台异常';
                    if (typeof alertElement != 'undefined') {
                        showDialog(alertElement);
                    }
                } else {
                    ajaxJsonp(url, data, cb, num);
                }
            }
        });
    }

    // 刷新滚动
    function onCompletion() {
        setTimeout(function() {
            myScroll.refresh();
        }, 0);
    }

    // 获取地址栏url后面的参数
    function getUrlParam(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
        var r = window.location.search.substr(1).match(reg); //匹配目标参数
        if (r != null) return unescape(r[2]);
        return null; //返回参数值
    }
})
